<?php

$user   = array(
    'username'   => 'teague',
    'First Name' => 'Teague',
    'Last Name'  => 'Sterling'
);
$format = "M. d, Y";

$prefix = "/home/student/667.16";

foreach(array(
    array(
        'name'  => $user['First Name'],
        'path'  => "/home/{$user['username']}",
        'alias' => "~",
        'type'  => 'user directory root'),
    array(
        'name'  => "Projects",
        'path'  => "/home/{$user['username']}/.wice-projects",
        'type'  => "projects directory root",
        'parse' => "projects_directory"),
    array(
        'name'  => 'System',
        'path'  => "/var/www/lib",
        'type'  => 'system directory root'),
    array(
        'name'  => "Public",
        'path'  => "/home",
        'type'  => "public directory root",
        'parse' => "public_directories")
) as $root) {
    $name = isset($root['alias']) ? $root['alias'] : $root['name'];
    $roots[$name] = $root;
}

$p = rtrim(preg_replace("|/+|", "/", $_GET['p']), "/");
$p = $p ? $p : "/";
$type = $_GET['t'];

$contents = array();

if($p == '/') {
    foreach($roots as $root) {
        $contents[] = array(
            'name' => $root['name'],
            'path' => isset($root['alias']) ? $root['alias'] : $root['name'],
            'type' => $root['type'],
            'date' => date($format));
    }
} elseif($p == "Projects") {
    $parent = $prefix.$roots['Projects']['path'];
    $dir = dir($parent);
    while(($entry = $dir->read()) !== false) {
        if($entry[0] == ".") continue;
        $path = "{$parent}/{$entry}";
        if(is_dir($path)) {
            $type = 'directory';
        } else {
            $type = 'project';
            $project = parse_ini_file($path, true);
            $project['group'] = isset($project['group']) ? explode(' ', $project['group']) : array();
            if($project['owner'] != $user['username'] && !in_array($user['username'], $project['group'])) {
                continue;
            }
            
            $location = $project['location'];
            if(strpos($location, $roots['~']['path']) === 0) {
                $location = str_replace($roots['~']['path'], "~", $location);
            } else {
                $location = str_replace($roots['Public']['path'], "/Public/", $location);
            }
            
            $extPath = basename($entry, '.wice.ini');
            
            $contents[] = array(
                'name'        => basename($entry, '.wice.ini'),
                'date'        => date($format, filemtime($path)),
                'path'        => $extPath,
                'type'        => 'local project',
                'external'    => $location,
                'owner'       => $project['owner'],
                'group'       => $project['group'],
                'visible'     => isset($project['visible']) ? $project['visible'] : 'private',
                'editable'    => isset($project['editable']) ? $project['editable'] : 'private',
                'description' => isset($project['description']) ? $project['description'] : '',
                'msroll'      => isset($project['MSROLL']) ? $project['MSROLL'] : array(),
                'rcoal'       => isset($project['RCOAL']) ? $project['RCOAL'] : array(),
                'best'        => isset($project['BEST']) ? $project['BEST'] : array(),
            );
        }
    }
    $dir->close();
} else {
    if($p[0] == "/") {
        $root    = explode("/", $p, 3);
        $section = $root[1];
        $path    = $root[2];
    } else {
        $root     = explode("/", $p, 2);
        $section  = $root[0];
        $path     = $root[1];
    }
    $parent   = "{$roots[$section]['path']}/{$path}";
    $parent   = rtrim($parent, "/");
    $dir      = dir($prefix.$parent);
    while(($entry = $dir->read()) !== false) {
        if($entry[0] == ".") continue;
        $path = "{$prefix}{$parent}/{$entry}";
        $type = $section == "~" ? "" : "local ";
        if(is_dir($path)) {
            $type .= 'directory';
        } else {
            $type .= 'file';
        }
        $contents[] = array(
            'name' => $entry,
            'path' => "{$p}/{$entry}",
            'type' => $type,
            'date' => date($format, filemtime($path))
        );
    }
    $dir->close();
}

$message = array(
    'base'     => $p,
    'contents' => $contents
);


echo json_encode($message);
